package com.github.limit.util;

import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @author: wangdejun
 * @create: 2019-10-21 13:56
 **/
public class RequestUtils {

    public static HttpServletRequest getRequest() {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();

        if (requestAttributes == null) {
            throw new UnsupportedOperationException("无法获取到HttpServletRequest，请检测是否在web项目中");
        }
        ServletRequestAttributes attributes = (ServletRequestAttributes) requestAttributes;
        HttpServletRequest request = attributes.getRequest();
        if (request == null) {
            throw new UnsupportedOperationException("无法获取到HttpServletRequest，请检测是否在web项目中");
        }
        return request;
    }
}
